# django
from django.db import models
from django.contrib.auth.models import User
# local
from pubblr.apps.product.models import Product


class Star(models.Model):
    """
    some docstring for Star class
    'Star' for anonymous and reviewer to rate product without giving a review
    """
    product = models.ForeignKey(Product)
    reviewer = models.ForeignKey(User, blank=True, null=True) # will support annonymous user later
    level = models.SmallIntegerField() # product level from -5 to +5
    add_datetime = models.DateTimeField(auto_now_add=True)
    ip = models.IPAddressField(default='127.0.0.1') # we'll make this auto detect later (derive from callable or something)

    def __unicode__(self):
        return '%s rates %s as %d' % (self.reviewer.username, self.product.name, self.level)
        
    class Meta:
        ordering = ['-id']
        

class Review(models.Model):
    """
    some docstring for Review class
    """
    product = models.ForeignKey(Product)
    reviewer = models.ForeignKey(User)
    body = models.TextField(blank=True)
    #pro = models.TextField() # will reactivate later
    #con = models.TextField() # will reactivate later
    star = models.ForeignKey(Star, blank=True, null=True) # From -5 to +5
    rating = models.SmallIntegerField(default=0) # Comment Rating *not product rating*
    add_datetime = models.DateTimeField(auto_now_add=True)
    lastest_rate_datetime = models.DateTimeField(auto_now_add=True)
    lastest_edit_datetime = models.DateTimeField(auto_now_add=True)
    ip = models.IPAddressField(default='127.0.0.1') # we'll make this auto detect later (derive from callable or something)

    def __unicode__(self):
        return '%s reviews on %s' % (self.reviewer.username, self.product.name)

    def incRating(self):
        self.rating += 1
        self.latest_rate_datetime = datetime.now()
        self.save()

    def decRating(self):
        self.rating -= 1
        self.latest_rate_datetime = datetime.now()
        self.save()

    class Meta:
        ordering = ['-id']
        
# todo:
# make default ip derive from request or something
# or design another approach in loggin ip
# MIDDLEWARE maybe ?
# more design on models is required
